/*************************************************************************
	> File Name: sqlite_operation.c
	> Author: 
	> Mail: 
	> Created Time: 2018年08月28日 星期二 17时16分36秒
 ************************************************************************/

#include <sqlite_operation.h>

//登录信息处理函数
int admin_data(admin *adm, fifo *p)
{
	int ret = 0;
	int fifo_fd;
	
	adm_state * adm_s = (adm_state *)malloc(sizeof(adm_state));
	memset(adm_s, 0, sizeof(adm_state));
	
	memcpy(adm_s->id, adm->id, strlen(adm->id));
	adm_s->state = 1;
	adm_s->sockID = p->sockID;
	
	ret = user_password_compare(adm->id, adm->password);

	if (OK == ret)
	{
		memcpy(p->buf, DLOK, strlen(DLOK));	
	}
	else
	{
		memcpy(p->buf, DLCW, strlen(DLCW));
	}
	memcpy(p->buf + strlen(DLOK), adm_s, sizeof(adm_state));
	
	fifo_fd = open(SOCKET_FIFO, O_RDWR);
	ret = write(fifo_fd, p, sizeof(fifo));
	close(fifo_fd);
		
	free(adm_s);
	return OK;
}

//保存数据处理函数
int save_data(server_to_client *stc)
{
	int ret = 0;
	ret = insert_record_info_table(stc);
	if (ERR == ret)
	{
		return ERR;
	}
	
	return OK;
}

//查询历史信息处理函数
int history_data(_time *tim)
{
	printf("tmie:%s %s\n", tim->nowDate, tim->nowTime);
	
	int ret = 0;
	int fifo_fd;
	fifo *p = (fifo *)malloc(sizeof(fifo));
	break_date *del;
	
	int count = 0;
	env_data env;
	
	break_date *list = create_break_date();	
	ret = search_record_info_table(list, tim->nowDate);
	if (1 == ret || -1 == ret)
	{
		fifo_fd = open(SOCKET_FIFO, O_RDWR);
		write(fifo_fd, CXSB, strlen(CXSB));
		close(fifo_fd);
		free(list);
		free(p);
		return -1;
	}
	
	printf("查询成功:%d\n", ret);

	while(list->last != NULL)
	{
		count = 0;
		memset(&env, 0, sizeof(env_data));
		
		while(count < COUNT && list->last != NULL)
		{
			env.tem += list->last->date.env_data.tem;
			env.hum += list->last->date.env_data.hum;
			env.lux += list->last->date.env_data.lux;
			count++;
			
			del = list->last;
			list->last = del->last;
			free(del);
		}
		
		env.tem = env.tem / count;
		env.hum = env.hum / count;
		env.lux = env.lux / count;
		//printf("%d\t%f\t%f\t%f\n", count, env.tem, env.hum, env.lux);
		memset(p, 0, sizeof(fifo));
		memcpy(p->buf, LSSJ, strlen(LSSJ));
		memcpy(p->buf + strlen(LSSJ), &env, sizeof(env_data));
		
		fifo_fd = open(SOCKET_FIFO, O_RDWR);
		write(fifo_fd, p, sizeof(fifo));
		close(fifo_fd);	
		usleep(100000);
	}
	//printf("查询完成:%p\n", list->last);
	memset(p, 0, sizeof(fifo));
	memcpy(p->buf, CXJS, strlen(CXJS));
	fifo_fd = open(SOCKET_FIFO, O_RDWR);
	write(fifo_fd, p, sizeof(fifo));
	close(fifo_fd);

	free(list);
	free(p);
	
	return OK;
}

//注册账号
int register_data(admin *adm)
{
	int ret = 0;
	int fifo_fd;
	
	fifo *p = (fifo *)malloc(sizeof(fifo));
	memset(p, 0, sizeof(fifo));
	
	ret = add_user_password_table(adm);
	if (OK == ret)
	{
		memcpy(p->buf, ZCOK, strlen(ZCOK));
	}
	else
	{
		memcpy(p->buf, ZCSB, strlen(ZCSB));
	}	
	fifo_fd = open(SOCKET_FIFO, O_RDWR);
	ret = write(fifo_fd, p, sizeof(fifo));
	close(fifo_fd);
	free(p);
	
	return OK;
}

//查询卡号是否存在
int search_card_number(int id)
{
	int rf_id = 0;
	int ret = 0;
	int fifo_fd;
	//printf("tian jia ka hao : id = %d\n", id);
	fifo *p = (fifo *)malloc(sizeof(fifo));
	memset(p, 0, sizeof(fifo));
	ret = cordid_compare(id);
	//printf("tian jia ka hao : ret = %d\n", ret);
	if (0 == ret)
	{
		rf_id = -1;
	}
	else
	{
		rf_id = 0;
	}

	memcpy(p->buf, MJKH, strlen(MJKH));
	memcpy(p->buf + strlen(MJKH), &rf_id, sizeof(int));
	
	fifo_fd = open(TOWARD_M0_FIFO, O_RDWR);
	ret = write(fifo_fd, &p->buf, BUF_SIZE - 1);
	close(fifo_fd);
	free(p);
	return OK;
}

//添加卡号
int add_card_id(int id)
{
	//printf("tian jia ka hao : id = %d\n", id);
	int ret = 0;
	int fifo_fd;
	
	fifo *p = (fifo *)malloc(sizeof(fifo));
	memset(p, 0, sizeof(fifo));
	
	ret = add_card_id_table(id);
	//printf("tian jia ka hao : ret = %d\n", ret);
	if (OK == ret)
	{
		memcpy(p->buf, KHOK, strlen(KHOK));
	}
	else
	{
		memcpy(p->buf, KHSB, strlen(KHSB));
	}
	fifo_fd = open(SOCKET_FIFO, O_RDWR);
	ret = write(fifo_fd, p, sizeof(fifo));
	close(fifo_fd);
	free(p);
	
	return OK;
}































